package cn.tedu.pet.product.service.impl;

import cn.tedu.pet.commons.ex.ServiceException;
import cn.tedu.pet.commons.pojo.vo.PageData;
import cn.tedu.pet.commons.util.PageInfoToPageDataConverter;
import cn.tedu.pet.commons.web.ServiceCode;
import cn.tedu.pet.product.mapper.OrderMapper;
import cn.tedu.pet.product.mapper.SKUMapper;
import cn.tedu.pet.product.mapper.SPUMapper;
import cn.tedu.pet.product.pojo.entity.Order;
import cn.tedu.pet.product.pojo.entity.SKU;
import cn.tedu.pet.product.pojo.entity.SPU;
import cn.tedu.pet.product.pojo.param.OrderAddNewParam;
import cn.tedu.pet.product.pojo.vo.OrderListItemVO;
import cn.tedu.pet.product.pojo.vo.SkuStandardVO;
import cn.tedu.pet.product.pojo.vo.SpuStandardVO;
import cn.tedu.pet.product.service.IOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Slf4j
@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SPUMapper spuMapper;
    @Autowired
    private SKUMapper skuMapper;


    @Override
    public void addNew(OrderAddNewParam orderAddNewParam){
        log.debug("开始处理【添加订单】的业务，参数：{}", orderAddNewParam);
            Order order = new Order();
//            order.setSpuId(orderAddNewParam.getSpuId());
//            order.setSkuId(orderAddNewParam.getSkuId());
            BeanUtils.copyProperties(orderAddNewParam,order);

            order.setOrderNumber(UUID.randomUUID().toString());
            order.setGmtCreate(LocalDateTime.now());
            order.setGmtModified(LocalDateTime.now());

        Long skuId = orderAddNewParam.getSkuId();
        SkuStandardVO skuStandardById = skuMapper.getStandardById(skuId);
        if (orderAddNewParam.getNumber()>skuStandardById.getStock()){
            String message = "添加订单失败，商品库存不足！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }

        SKU sku = new SKU();
        BeanUtils.copyProperties(skuStandardById,sku);
        sku.setStock(skuStandardById.getStock()-orderAddNewParam.getNumber());
        int count = skuMapper.updateById(sku);
        if (count != 1){
            String message = "添加订单失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }

        int rows = orderMapper.insert(order);
        if (rows != 1){
            String message = "添加订单失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }

        log.debug("将新的订单数据写入到数据库，完成！");




    }

    @Override
    public PageData<OrderListItemVO> list(Integer pageNum) {
        Integer pageSize = 10;
        return list(pageNum,pageSize);
    }

    @Override
    public PageData<OrderListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询订单列表】的业务，页码：{}，每页记录数：{}",pageNum,pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<OrderListItemVO> list = orderMapper.list();
        PageInfo<OrderListItemVO> pageInfo = new PageInfo<>(list);
        PageData<OrderListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        log.debug("查询完成，即将返回：{}",pageData);
        return pageData;
    }
}
